﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ModelQLKS.Model;
namespace ServiceQLKS
{
    public enum ErrorCodeQLKS
    {
        CODE_ERROR,
        CODE_NHANVIEN_GETALL_SUCCESS, CODE_NHANVIEN_GETALL_FAIL,
        CODE_NHANVIEN_DELETE_SUCCESS, CODE_NHANVIEN_DELETE_FAIL,
        CODE_NHANVIEN_INSERTUPDATE_SUCCESS, CODE_NHANVIEN_INSERTUPDATE_FAIL,
        CODE_NHANVIEN_CHECKLOGIN_SUCCESS, CODE_NHANVIEN_CHECKLOGIN_FAIL,

        CODE_LOAIPHONG_GETALL_SUCCESS, CODE_LOAIPHONG_GETALL_FAIL,
        CODE_LOAIPHONG_DELETE_SUCCESS, CODE_LOAIPHONG_DELETE_FAIL,
        CODE_LOAIPHONG_INSERTUPDATE_SUCCESS, CODE_LOAIPHONG_INSERTUPDATE_FAIL,

        CODE_PHONG_GETALL_SUCCESS, CODE_PHONG_GETALL_FAIL,
        CODE_PHONG_DELETE_SUCCESS, CODE_PHONG_DELETE_FAIL,
        CODE_PHONG_INSERTUPDATE_SUCCESS, CODE_PHONG_INSERTUPDATE_FAIL,

        CODE_THIETBI_GETALL_SUCCESS, CODE_THIETBI_GETALL_FAIL,
        CODE_THIETBI_DELETE_SUCCESS, CODE_THIETBI_DELETE_FAIL,
        CODE_THIETBI_INSERTUPDATE_SUCCESS, CODE_THIETBI_INSERTUPDATE_FAIL,

        CODE_TBTHIETBI_GETALL_SUCCESS, CODE_TBTHIETBI_GETALL_FAIL,
        CODE_TBTHIETBI_DELETE_SUCCESS, CODE_TBTHIETBI_DELETE_FAIL,
        CODE_TBTHIETBI_INSERTUPDATE_SUCCESS, CODE_TBTHIETBI_INSERTUPDATE_FAIL,

        CODE_KHACHHANG_GETALL_SUCCESS, CODE_KHACHHANG_GETALL_FAIL,
        CODE_KHACHHANG_DELETE_SUCCESS, CODE_KHACHHANG_DELETE_FAIL,
        CODE_KHACHHANG_INSERTUPDATE_SUCCESS, CODE_KHACHHANG_INSERTUPDATE_FAIL,
    }
    public enum OPTCodeQLKS
    {
        OPTCode_NHANVIEN_GETALL,
        OPTCode_NHANVIEN_DELETE,
        OPTCode_NHANVIEN_INSERTUPDATE,
        OPTCode_NHANVIEN_CHECKLOGIN,

        OPTCode_LOAIPHONG_GETALL,
        OPTCode_LOAIPHONG_INSERTUPDATE,
        OPTCode_LOAIPHONG_DELETE,

        OPTCode_PHONG_GETALL,
        OPTCode_PHONG_INSERTUPDATE,
        OPTCode_PHONG_DELETE,

        OPTCode_THIETBI_GETALL,
        OPTCode_THIETBI_INSERTUPDATE,
        OPTCode_THIETBI_DELETE,

        OPTCode_TBTHIETBI_GETALL,
        OPTCode_TBTHIETBI_INSERTUPDATE,
        OPTCode_TBTHIETBI_DELETE,

        OPTCode_KHACKHANG_GETALL,
        OPTCode_KHACKHANG_INSERTUPDATE,
        OPTCode_KHACKHANG_DELETE,
    }

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class ServiceQLKS : IServiceQLKS
    {
        QLKSDB _db = new QLKSDB();

        #region Cac ham xu ly lien quan den NHANVIEN
        public ErrorCodeQLKS doparserPerson(OPTCodeQLKS code, NHANVIEN nhanvieninput, out IList<NHANVIEN> nhanvienout)
        {
            switch (code)
            {
                case OPTCodeQLKS.OPTCode_NHANVIEN_GETALL:
                    IList<NHANVIEN> Listpersonall = getPersonAll();
                    if (Listpersonall != null && Listpersonall.Count > 0)
                    {
                        nhanvienout = Listpersonall;
                        return ErrorCodeQLKS.CODE_NHANVIEN_GETALL_SUCCESS;
                    }
                    else
                    {
                        nhanvienout = null;
                        return ErrorCodeQLKS.CODE_NHANVIEN_GETALL_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_NHANVIEN_DELETE:
                    nhanvienout = null;
                    return deleteperson(nhanvieninput);
                case OPTCodeQLKS.OPTCode_NHANVIEN_INSERTUPDATE:
                    nhanvienout = insertupdateperson(nhanvieninput);
                    if (nhanvienout != null && nhanvienout.Count > 0)
                    {
                        return ErrorCodeQLKS.CODE_NHANVIEN_INSERTUPDATE_SUCCESS;
                    }
                    else
                    {
                        return ErrorCodeQLKS.CODE_NHANVIEN_INSERTUPDATE_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_NHANVIEN_CHECKLOGIN:
                    nhanvienout = null;
                    return checklogin(nhanvieninput);
                default:
                    nhanvienout = null;
                    return ErrorCodeQLKS.CODE_ERROR;
            }
        }

        private ErrorCodeQLKS deleteperson(NHANVIEN model)
        {
            var sp = _db.pr_del_NhanVien(model.MSNV);
            sp.Execute();
            return ErrorCodeQLKS.CODE_NHANVIEN_DELETE_SUCCESS;
        }

        private IList<NHANVIEN> insertupdateperson(NHANVIEN model)
        {
            var sp = _db.pr_ins_NV(model.OPTION, model.MSNV, model.HOTENNV, model.MatKhau, model.NGAYSINHNV, model.GIOITINHNV, model.DIACHINV
                , model.DIENTHOAINV, model.NGAYVAOLAM, model.HINH);
            var result = sp.ExecuteTypedList<NHANVIEN>();
            return result;

        }

        private IList<NHANVIEN> getPersonAll()
        {
            var sp = _db.pr_list_NV();
            var result = sp.ExecuteTypedList<NHANVIEN>();
            return result;
        }

        private ErrorCodeQLKS checklogin(NHANVIEN model)
        {
            var sp = _db.pr_check_login(model.MSNV, model.MatKhau);
            IList<NHANVIEN> lstnv = sp.ExecuteTypedList<NHANVIEN>();
            if (lstnv != null)
            {
                return ErrorCodeQLKS.CODE_NHANVIEN_CHECKLOGIN_SUCCESS;
            }
            else
            {
                return ErrorCodeQLKS.CODE_NHANVIEN_CHECKLOGIN_FAIL;
            }
        }
        #endregion

        
        #region Cac Ham xu ly lien quan toi LOAIPHONG
        public ErrorCodeQLKS doparserLoaiPhong(OPTCodeQLKS code, LOAIPHONG loaiphonginput, out IList<LOAIPHONG> loaiphongout)
        {
            switch (code)
            {
                case OPTCodeQLKS.OPTCode_LOAIPHONG_GETALL:
                    IList<LOAIPHONG> ListLoaiPhongAll = getAllLoaiPhong();
                    if (ListLoaiPhongAll != null && ListLoaiPhongAll.Count > 0)
                    {
                        loaiphongout = ListLoaiPhongAll;
                        return ErrorCodeQLKS.CODE_LOAIPHONG_GETALL_SUCCESS;
                    }
                    else
                    {
                        loaiphongout = null;
                        return ErrorCodeQLKS.CODE_LOAIPHONG_GETALL_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_LOAIPHONG_INSERTUPDATE:
                    IList<LOAIPHONG> ListLoaiPhong = InsertUpdateLoaiPhong(loaiphonginput);
                    if (ListLoaiPhong != null && ListLoaiPhong.Count > 0)
                    {
                        loaiphongout = ListLoaiPhong;
                        return ErrorCodeQLKS.CODE_LOAIPHONG_INSERTUPDATE_SUCCESS;
                    }
                    else
                    {
                        loaiphongout = null;
                        return ErrorCodeQLKS.CODE_LOAIPHONG_INSERTUPDATE_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_LOAIPHONG_DELETE:
                    loaiphongout = null;
                    return deleteLoaiPhong(loaiphonginput);
                default:
                    loaiphongout = null;
                    return ErrorCodeQLKS.CODE_ERROR;
            }
        }

        private IList<LOAIPHONG> getAllLoaiPhong()
        {
            var sp = _db.pr_list_LoaiPhong();
            var result = sp.ExecuteTypedList<LOAIPHONG>();
            return result;
        }

        private IList<LOAIPHONG> InsertUpdateLoaiPhong(LOAIPHONG model)
        {
            var sp = _db.pr_ins_LoaiPhong(model.MSLOAIPHONG, model.TENLOAIPHONG, model.GIATIEN);
            var result = sp.ExecuteTypedList<LOAIPHONG>();
            return result;
        }

        private ErrorCodeQLKS deleteLoaiPhong(LOAIPHONG model)
        {
            var sp = _db.pr_del_LoaiPhong(model.MSLOAIPHONG);
            sp.Execute();
            return ErrorCodeQLKS.CODE_NHANVIEN_DELETE_SUCCESS;
        } 
        #endregion


        #region Cac ham xu ly lien quan toi DMPHONG
        public ErrorCodeQLKS doparserPhong(OPTCodeQLKS code, DMPHONG PhongInput, out IList<DMPHONG> PhongOutput)
        {
            switch (code)
            {
                case OPTCodeQLKS.OPTCode_PHONG_GETALL:
                    IList<DMPHONG> listallphong = getAllPhong();
                    if (listallphong != null && listallphong.Count > 0)
                    {
                        PhongOutput = listallphong;
                        return ErrorCodeQLKS.CODE_PHONG_GETALL_SUCCESS;
                    }
                    else
                    {
                        PhongOutput = null;
                        return ErrorCodeQLKS.CODE_PHONG_GETALL_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_PHONG_INSERTUPDATE:
                    IList<DMPHONG> listphong = InsertOrUpdatePhong(PhongInput);
                    if (listphong != null && listphong.Count > 0)
                    {
                        PhongOutput = listphong;
                        return ErrorCodeQLKS.CODE_PHONG_INSERTUPDATE_SUCCESS;
                    }
                    else
                    {
                        PhongOutput = null;
                        return ErrorCodeQLKS.CODE_PHONG_INSERTUPDATE_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_PHONG_DELETE:
                    PhongOutput = null;
                    return deletePhong(PhongInput);
                default:
                    PhongOutput = null;
                    return ErrorCodeQLKS.CODE_ERROR;
            }
        }

        private IList<DMPHONG> getAllPhong()
        {
            var sp = _db.pr_list_Phong();
            return sp.ExecuteTypedList<DMPHONG>();
        }

        private IList<DMPHONG> InsertOrUpdatePhong(DMPHONG model)
        {
            var sp = _db.pr_ins_Phong(model.MSPHONG, model.MSLOAIPHONG, model.TINHTRANG);
            return sp.ExecuteTypedList<DMPHONG>();
        }

        private ErrorCodeQLKS deletePhong(DMPHONG model)
        {
            var sp = _db.pr_del_Phong(model.MSPHONG);
            sp.Execute();
            return ErrorCodeQLKS.CODE_PHONG_DELETE_SUCCESS;
        } 
        #endregion


        #region Cac ham xu ly lien quan toi ThietBI
        public ErrorCodeQLKS doparserThietBi(OPTCodeQLKS code, THIETBI_DV ThietBiInput, out IList<THIETBI_DV> ThietBiOutput)
        {
            switch (code)
            {
                case OPTCodeQLKS.OPTCode_THIETBI_GETALL:
                    IList<THIETBI_DV> lstThietBiALL = getAllThietBi();
                    if (lstThietBiALL != null && lstThietBiALL.Count > 0)
                    {
                        ThietBiOutput = lstThietBiALL;
                        return ErrorCodeQLKS.CODE_THIETBI_GETALL_SUCCESS;
                    }
                    else
                    {
                        ThietBiOutput = null;
                        return ErrorCodeQLKS.CODE_THIETBI_GETALL_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_THIETBI_INSERTUPDATE:
                    IList<THIETBI_DV> lstThietBi = InsertOrUpdateThietBi(ThietBiInput);
                    if (lstThietBi != null && lstThietBi.Count > 0)
                    {
                        ThietBiOutput = lstThietBi;
                        return ErrorCodeQLKS.CODE_THIETBI_INSERTUPDATE_SUCCESS;
                    }
                    else
                    {
                        ThietBiOutput = null;
                        return ErrorCodeQLKS.CODE_THIETBI_INSERTUPDATE_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_THIETBI_DELETE:
                    ThietBiOutput = null;
                    return deleteThietBi(ThietBiInput);
                default:
                    ThietBiOutput = null;
                    return ErrorCodeQLKS.CODE_ERROR;
            }
        }

        private IList<THIETBI_DV> getAllThietBi()
        {
            var sp = _db.pr_list_ThietBi();
            return sp.ExecuteTypedList<THIETBI_DV>();
        }

        private IList<THIETBI_DV> InsertOrUpdateThietBi(THIETBI_DV model)
        {
            var sp = _db.pr_ins_THIETBI_DV(model.MSTB, model.TENTHIETBI, model.DVT, model.GIATB, model.THIETBICODINH);
            return sp.ExecuteTypedList<THIETBI_DV>();
        }

        private ErrorCodeQLKS deleteThietBi(THIETBI_DV model)
        {
            var sp = _db.pr_del_THIETBI_DV(model.MSTB);
            sp.Execute();
            return ErrorCodeQLKS.CODE_THIETBI_DELETE_SUCCESS;
        } 
        #endregion


        #region Cac ham xu ly lien qua toi TBThietBi
        public ErrorCodeQLKS doparserTBThietBi(OPTCodeQLKS code, TRANGBITRANGTB TBThietBiInput, out IList<TRANGBITRANGTB> TBThietBiOutput)
        {
            switch (code)
            {
                case OPTCodeQLKS.OPTCode_TBTHIETBI_GETALL:
                    IList<TRANGBITRANGTB> lstTBThietBiALL = getAllTBThietBi();
                    if (lstTBThietBiALL != null && lstTBThietBiALL.Count > 0)
                    {
                        TBThietBiOutput = lstTBThietBiALL;
                        return ErrorCodeQLKS.CODE_TBTHIETBI_GETALL_SUCCESS;
                    }
                    else
                    {
                        TBThietBiOutput = null;
                        return ErrorCodeQLKS.CODE_TBTHIETBI_GETALL_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_TBTHIETBI_INSERTUPDATE:
                    IList<TRANGBITRANGTB> lstTBThietBi = InsertOrUpdateTBThietBi(TBThietBiInput);
                    if (lstTBThietBi != null && lstTBThietBi.Count > 0)
                    {
                        TBThietBiOutput = lstTBThietBi;
                        return ErrorCodeQLKS.CODE_TBTHIETBI_INSERTUPDATE_SUCCESS;
                    }
                    else
                    {
                        TBThietBiOutput = null;
                        return ErrorCodeQLKS.CODE_TBTHIETBI_INSERTUPDATE_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_TBTHIETBI_DELETE:
                    TBThietBiOutput = null;
                    return DeleteTBThietBi(TBThietBiInput);
                default:
                    TBThietBiOutput = null;
                    return ErrorCodeQLKS.CODE_ERROR;
            }
        }

        private IList<TRANGBITRANGTB> getAllTBThietBi()
        {
            var sp = _db.pr_list_TBTrangThietBi();
            return sp.ExecuteTypedList<TRANGBITRANGTB>();
        }

        private IList<TRANGBITRANGTB> InsertOrUpdateTBThietBi(TRANGBITRANGTB model)
        {
            var sp = _db.pr_ins_TBTrangTB(model.MSLOAIPHONG, model.MSTB, model.SOLUONG);
            return sp.ExecuteTypedList<TRANGBITRANGTB>();
        }

        private ErrorCodeQLKS DeleteTBThietBi(TRANGBITRANGTB model)
        {
            var sp = _db.pr_del_TBTrangTB(model.MSLOAIPHONG, model.MSTB);
            sp.Execute();
            return ErrorCodeQLKS.CODE_TBTHIETBI_DELETE_SUCCESS;
        } 
        #endregion


        public ErrorCodeQLKS doparserKhacHang(OPTCodeQLKS code, KHACHHANG KhachHangInput, out IList<KHACHHANG> KhachHangOutput)
        {
            switch (code)
            {
                case OPTCodeQLKS.OPTCode_KHACKHANG_GETALL:
                    IList<KHACHHANG> lstkhall = getAllKhachHang();
                    if (lstkhall != null && lstkhall.Count > 0)
                    {
                        KhachHangOutput = lstkhall;
                        return ErrorCodeQLKS.CODE_KHACHHANG_GETALL_SUCCESS;
                    }
                    else
                    {
                        KhachHangOutput = null;
                        return ErrorCodeQLKS.CODE_KHACHHANG_GETALL_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_KHACKHANG_INSERTUPDATE:
                    IList<KHACHHANG> lstkh = InsertorUpdateKH(KhachHangInput);
                    if (lstkh != null && lstkh.Count > 0)
                    {
                        KhachHangOutput = lstkh;
                        return ErrorCodeQLKS.CODE_KHACHHANG_INSERTUPDATE_SUCCESS;
                    }
                    else
                    {
                        KhachHangOutput = null;
                        return ErrorCodeQLKS.CODE_KHACHHANG_INSERTUPDATE_FAIL;
                    }
                case OPTCodeQLKS.OPTCode_KHACKHANG_DELETE:
                    KhachHangOutput = null;
                    return DeleteKhachhang(KhachHangInput);
                default:
                    KhachHangOutput = null;
                    return ErrorCodeQLKS.CODE_ERROR;
            }
        }
        private IList<KHACHHANG> getAllKhachHang()
        {
            var sp = _db.pr_list_KH();
            return sp.ExecuteTypedList<KHACHHANG>();
        }
        private IList<KHACHHANG> InsertorUpdateKH(KHACHHANG model)
        {
            var sp = _db.pr_ins_KH(model.MSKH, model.HOTENKH, model.GIOITINHKH, model.DIACHIKH, model.CMND, model.DIENTHOAIKH);
            return sp.ExecuteTypedList<KHACHHANG>();
        }

        private ErrorCodeQLKS DeleteKhachhang(KHACHHANG model)
        {
            var sp = _db.pr_del_KH(model.MSKH);
            sp.Execute();
            return ErrorCodeQLKS.CODE_KHACHHANG_DELETE_SUCCESS;
        }
    }
}
